package cn.dw.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Test;


import cn.dw.db.DBO;
import cn.dw.db.JdbcUtil;
import cn.dw.model.Reader;
import cn.dw.util.AddStudentUntil;

public class JustOne {
	//	连接对象
	private static Connection conn1=null;
	//	发送sql语句
	private static PreparedStatement stmt1=null;
	//	sql返回的结果集
	private static ResultSet rs1=null;

	//	将基础数据加入
	
	@Test
	public  void test() {
		DBO db = new DBO();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sdf.format(new Date());
		String sql = "select * from vDzxxb where 状态位='N'";
		try {
			conn1 = JdbcUtil.getConnection();
			stmt1 = conn1.prepareStatement(sql);
			rs1 = stmt1.executeQuery();
			List<Reader> list = new ArrayList<Reader>();

			PreparedStatement preparedStatement2 = null;
			Connection con2=db.getCon();
			con2.setAutoCommit(false);
			String sql2 = "insert into dzxxb (xlh,dzzh) values (?,?)";
			preparedStatement2=con2.prepareStatement(sql2);
			int i=0;

			while(rs1.next()){
				String xlh = rs1.getString(1).replaceAll(" +","");//标识列
				String dzzh = rs1.getString(2).replaceAll(" +","");//读者证号
				String dzxm = rs1.getString(3).replaceAll(" +","");//读者姓名
				String dzdw = rs1.getString(4).replaceAll(" +","");//读者单位
				Reader g=new Reader();
				g.setXlh(xlh);
				g.setDzzh(dzzh);
				g.setDzxm(dzxm);
				g.setDzdw(dzdw);
				System.out.println(g.toString());
				list.add(g);

				preparedStatement2.setString(1, xlh);
				preparedStatement2.setString(2, dzzh);
				preparedStatement2.addBatch();
				i++;
				if(i%500==0){//可以设置不同的大小；如50，100，500，1000等等      
					preparedStatement2.executeBatch();      
					con2.commit();      
					preparedStatement2.clearBatch();      
				}
			}
			preparedStatement2.executeBatch();      
			con2.commit();      
			preparedStatement2.clearBatch(); 
			db.closed();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.close(conn1, stmt1,rs1);
		}


	}
}